const App = {
    data() {
        return {
            photo,
            ACCESSTOKEN: '',
            loading: false,
            formData: {},
        }
    },
    beforeCreate() {
        function setRem() {
            const designWidth = 750; // 设计稿宽度（rpx）
            const maxWidth = 540; // 可选：限制最大宽度（如手机端常见值）
            const screenWidth = Math.min(window.innerWidth, maxWidth);
            const scale = screenWidth / designWidth;
            document.documentElement.style.fontSize = scale + 'px';
        }
        setRem();//1rem = 0.5px 对标小程序在 375px 宽的屏幕上，1rpx = 0.5px（因为 750rpx / 375px = 2）
        window.addEventListener('resize', setRem);
    },
    mounted() {
        let URL = window.location.href;
        this.ACCESSTOKEN = getQueryObject(URL, 'ACCESSTOKEN');
        let id = getQueryObject(URL, 'nodeKey');
        this.getList(id);
    },
    computed: {
        submitText() {
            const { Registration_Start_Datetime, Registration_End_Datetime, Activity_Datetime, Activity_End_Datetime } = this.formData;
            let obj = { is:false, label:'' }, now = +new Date();
            if ( now > +new Date(Registration_Start_Datetime) && now < +new Date(Registration_End_Datetime)) {
                obj.is = true;
                obj.label = '报名'
            }
            return str;
        }
    },
    methods: {
        getList(id) {
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
                method: "post",
                data: {
                    SQLSerialNumber: 't_village_activities_mini2',
                    Condition: window.btoa(unescape(encodeURIComponent(id)))
                },
                success: (res) => {
                    if (res.length > 0) {
                        this.formData = res[0];
                    }
                },
                complete: (XMLHttpRequest) => {
                    if (XMLHttpRequest.status == 302) {
                        wx.miniProgram.navigateTo({
                            url: miniProgram,
                        })
                    }
                },
            });
        },
        async handleSubmit() {
            const allValid = await this.validateAllForms();
            if (allValid && !this.loading) {
                this.loading = true;
                let lJSONObject_DataSet = {
                    Add:{
                        '':[this.form]
                    }
                }
                this.submitSave(lJSONObject_DataSet, (res) => {
                    if (res.Status == 'success') {
                        window.history.go(-1);
                    }
                    this.loading = false;
                })
            }
        },
        submitSave(Data, response) {
            const websocket = new WebSocketClient();
            websocket.init({
                url: ws + '/generalSave/' + getCookie('token'),
                onopen: () => {
                    let lJSONObject_DataSet = {
                        Data, AccessToken: getCookie('token')
                    }
                    websocket.send(JSON.stringify(lJSONObject_DataSet));
                },
                onmessage: (data) => {
                    try {
                        const res = JSON.parse(data);
                        websocket.close();
                        if (res.REDIRECT && res.REDIRECT == 'REDIRECT') {//重定向
                            wx.miniProgram.navigateTo({
                                url: miniProgram,
                            })
                        } else {
                            response ? response(res) : null;
                        }
                    } catch (error) { }
                },
            })
        }
    },
};

const { createApp } = Vue;
const app = createApp(App);
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}
const ElMessage = ElementPlus.ElMessage;
app.use(ElementPlus, { locale: zhCn });
app.mount("#app");